﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using OnlineVideos.Utilities;
using Common.Library;
using OnlineVideos.View_Models;
using OnlineVideos.Entities;
using OnlineVideos.Data;
using OnlineVideos;
using Common.Utilities;
using System.Threading.Tasks;

namespace OnlineVideos
{

    public partial class VideoGameDescription : PhoneApplicationPage
    {
        List<VideoGameProperties> Gamelist = null;
        public VideoGameDescription()
        {
            InitializeComponent();
            Gamelist = new List<VideoGameProperties>();
            Loaded += new RoutedEventHandler(WeaponsDescription_Loaded);
        }
        private void LoadAds()
        {
            try
            {
                PageHelper.LoadAdControl(LayoutRoot, adstaWeapon, 1);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in LoadAds Method In SongDetails file", ex);
                string excepmess = "Exception in LoadAds Method In SongDetails file.\n\n" + ex.Message + "\n\n StackTrace :- \n" + ex.StackTrace;
            }

        } 
        void WeaponsDescription_Loaded(object sender, RoutedEventArgs e)
        {
            LoadAds();
            string id = "";
            string wid = "";
            string type = "";
            if (NavigationContext.QueryString.TryGetValue("wid", out wid))
            {
                if (NavigationContext.QueryString.TryGetValue("type", out type))
                { }
                if (NavigationContext.QueryString.TryGetValue("id", out id))

                    LayoutRoot.Background = ShowDetail.LoadShowPivotBackground(Convert.ToInt32(id));
                Gamelist = Loaddescription(wid, id, type);
                if (Gamelist.Count > 0)
                {
                    tblkmsg.Visibility = Visibility.Collapsed;

                }
                else
                {
                    tblkmsg.Visibility = Visibility.Visible;
                }
            }
        }

        private List<VideoGameProperties> Loaddescription(string id, string showid, string type)
        {
            if (type == "Weapon")
            {
                LoadWeapons(id, showid);
            }
            if (type == "Vehicle")
            {
                loadVechiles(id, showid);
            }
            if (type == "Achievements")
            {
                loadAchievements(id, showid);
            }
            if (type == "Misions")
            {
                loadMissions(id, showid);
            } 
            if (type == "Cast")
            {
                loadCast(id, showid);
            }
            if (type == "Controls")
            {
                loadControls(id, showid);
            }

            return Gamelist;
        }

        private void loadControls(string id, string showid)
        {
            //OnlineVideoDataContext context = new OnlineVideoDataContext(Constants.DatabaseConnectionString);
            int conid = Convert.ToInt32(id);
            int showid1 = Convert.ToInt32(showid);
            var descList = Task.Run(async()=>await Constants.connection.Table<GameControls>().Where(i=>i.ControlId ==conid & i.ShowID == showid1).ToListAsync()).Result;
            foreach (var item in descList)
            {
                VideoGameProperties objvideoprop = new VideoGameProperties();
                tblkVideosTitle.Text = item.ControlName;
                objvideoprop.Name = item.ControlName;
                if (item.Controldescription != string.Empty)
                {
                    tblkDescription.Text = item.Controldescription;
                    Gamelist.Add(objvideoprop);
                }

            }
        }

        private void loadCast(string id, string showid)
        {
            //OnlineVideoDataContext context = new OnlineVideoDataContext(Constants.DatabaseConnectionString);
            int personid=Convert.ToInt32(id);
            var descList =Task.Run(async()=>await Constants.connection.Table<CastProfile>().Where(i=>i.PersonID == personid).ToListAsync()).Result;
            foreach (var item in descList)
            {
                VideoGameProperties objvideoprop = new VideoGameProperties();
                tblkVideosTitle.Text = item.Name;
                objvideoprop.Name = item.Name;
                if (item.Description != string.Empty)
                {
                    tblkDescription.Text = item.Description;
                    Gamelist.Add(objvideoprop);
                }

            }
        }

        private void loadMissions(string id, string showid)
        {
            //OnlineVideoDataContext context = new OnlineVideoDataContext(Constants.DatabaseConnectionString);
            int mid=Convert.ToInt32(id);
              int sid=Convert.ToInt32(showid);
            var descList =Task.Run(async()=>await Constants.connection.Table<GameMissions>().Where(i=>i.MissionId == mid & i.ShowID == sid).ToListAsync()).Result;
            foreach (var item in descList)
            {
                VideoGameProperties objvideoprop = new VideoGameProperties();
                tblkVideosTitle.Text = item.MissionName;
                objvideoprop.Name = item.MissionName;
                if (item.Missiondescription != string.Empty)
                {
                    string data = item.Walkthrough;
                    if (data != null)
                    {
                        tblkname.Visibility = Visibility.Visible;
                        tblkvalue.Visibility = Visibility.Visible;
                        tblkname.Text = "Walkthrough : ";
                        tblkvalue.Text = item.Walkthrough;
                    }
                    tblkDescription.Text = item.Missiondescription;
                    Gamelist.Add(objvideoprop);
                }

            }
        }


        private void loadAchievements(string id, string showid)
        {
            int aid = Convert.ToInt32(id);
            int sid = Convert.ToInt32(showid);
            //OnlineVideoDataContext context = new OnlineVideoDataContext(Constants.DatabaseConnectionString);
            var descList =Task.Run(async()=>await Constants.connection.Table<GameAchievement>().Where(i=>i.AchievementId ==aid & i.ShowID == sid).ToListAsync()).Result;
            foreach (var item in descList)
            {
                VideoGameProperties objvideoprop = new VideoGameProperties();
                tblkVideosTitle.Text = item.AchievementName;
                objvideoprop.Name = item.AchievementName;
                if (item.AchievementDescription != string.Empty)
                {
                    string data = item.Points;
                    if (data != null)
                    {
                        tblkname.Visibility = Visibility.Visible;
                        tblkvalue.Visibility = Visibility.Visible;
                        tblkname.Text = "Points : ";
                        tblkvalue.Text = item.Points;
                    }
                    tblkDescription.Text = item.AchievementDescription;
                    Gamelist.Add(objvideoprop);
                }

            }
        }

        private void loadVechiles(string id, string showid)
        {
            int vid = Convert.ToInt32(id);
            int sid = Convert.ToInt32(showid);
            //OnlineVideoDataContext context = new OnlineVideoDataContext(Constants.DatabaseConnectionString);
            var descList =Task.Run(async()=>await Constants.connection.Table<GameVehicles>().Where(i=>i.VehicleID == vid & i.ShowID == sid).ToListAsync()).Result;
            foreach (var item in descList)
            {
                VideoGameProperties objvideoprop = new VideoGameProperties();
                tblkVideosTitle.Text = item.Name;
                objvideoprop.Name = item.Name;
                if (item.Description != string.Empty)
                {
                    tblkDescription.Text = item.Description;
                    Gamelist.Add(objvideoprop);
                }
            }
        }

        private void LoadWeapons(string id, string showid)
        {
              int wid = Convert.ToInt32(id);
            int sid = Convert.ToInt32(showid);
            //OnlineVideoDataContext context = new OnlineVideoDataContext(Constants.DatabaseConnectionString);
           var descList =Task.Run(async()=>await Constants.connection.Table<GameWeapons>().Where(i=>i.WeaponID == wid & i.ShowID == sid).ToListAsync()).Result;
            foreach (var item in descList)
            {
                VideoGameProperties objvideoprop = new VideoGameProperties();
                tblkVideosTitle.Text = item.Name;
                objvideoprop.Name = item.Name;
                if (item.Description != string.Empty)
                {
                    tblkDescription.Text = item.Description;
                    Gamelist.Add(objvideoprop);
                }
            }
        }
       
    }

}